package com.cxs.controller;

import com.cxs.model.AirQualityIndex;
import com.cxs.service.AirQualityIndexService;
import cxs.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * @Author:cxs
 * @Date:2021/9/30  14:32
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/airQualityIndex")
@CrossOrigin(allowCredentials = "true")
public class AirQualityIndexController {
    @Autowired
    private AirQualityIndexService airQualityIndexService;

    @RequestMapping("/listPage/{pageNum}/{pageSize}/{id}")
    @ResponseBody
    public Object getAirQualityIndex(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @PathVariable("id") Integer id){
        if(pageNum == null) pageNum = 1;
        if(pageSize == null) pageSize = 10;
        if(id != null && id == -1){
            return Result.OK("请求成功",airQualityIndexService.listAllByPage(pageNum,pageSize));
        }else{
            return Result.OK("Ok",airQualityIndexService.search(id,pageNum,pageSize));
        }
    }

    @RequestMapping("/editpre")
    public String editpre(Integer id, Model model){
        if(id == null){
            model.addAttribute("msg","参数错误");
            return "message";
        }
        AirQualityIndex airQualityIndex = airQualityIndexService.getOneById(id);
        model.addAttribute("data",airQualityIndex);
        return "forward:/edit.jsp";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(AirQualityIndex obj){
        if(obj == null) return Result.failare("异常");
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
        try {
            if(obj.getReqTime() != null) obj.setMonitorTime(sdf.parse(obj.getReqTime()));
            obj.setLastModifityTime(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.failare("时间有误");
        }
        return airQualityIndexService.update(obj);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(AirQualityIndex obj){
        if(obj == null) return Result.failare("异常");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if(obj.getReqTime() != null) obj.setMonitorTime(sdf.parse(obj.getReqTime()));
            obj.setLastModifityTime(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.failare("时间有误");
        }
        return airQualityIndexService.add(obj);
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Integer id){
        if(id == null) return Result.failare("删除失败");
        return airQualityIndexService.delete(id);
    }
}
